// index.js
// 获取应用实例
const app = getApp()

Page({
	data: {
		userInfo: '',
		callphone: '',
		count:0,
		wx_nickname: '',
		wx_avatar: '',
		role:0
	},
	onExit() {
		wx.clearStorage({
			success(res) {
				wx.redirectTo({
					url: '/pages/login/index',
				})
			}
		});
		app.globalData.role = 0;
		app.globalData.token = '';
		app.globalData.code='';
		app.globalData.userInfo= null;
		app.globalData.records= '';
		app.globalData.token= '';
		app.globalData.time= '';
		app.globalData.lat='';
		app.globalData.lng='';
	},
	onLoad(){
		this.setData({
			role:app.globalData.role
		})
	},
	onShow() {
		
		if(app.globalData.records){
			this.setData({
				count: app.globalData.records.apply_num,
				callphone: app.globalData.records.call_phone,
				wx_nickname: app.globalData.records.wx_nickname,
				wx_avatar: app.globalData.records.wx_avatar
			})
		}
	},
	onGetUserInfo() {
		let that = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: app.globalData.baseUrl + '/api/user/getUserInfo',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			success: function(res) {
				wx.hideLoading()

				
			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	onGetWxInfo() {
		var that = this;
		wx.showModal({
			title: '提示',
			content: '是否授权获取用户信息？',
			success(res) {
				if (res.confirm) {
					that.getUserProfile()
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})
	},
	onGetCocact() {
		if (app.globalData.records) {
			wx.showModal({
				title: '联系平台',
				content: app.globalData.records.platform_phone,
				success(res) {
					if (res.confirm) {
						wx.makePhoneCall({
							phoneNumber: app.globalData.records.platform_phone //仅为示例，并非真实的电话号码
						})
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})
			return false
		}
		
	},
	getUserProfile(e) {
		var that = this;
		// 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
		// 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
		wx.getUserProfile({
			desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			success: (res) => {
				console.log(res)
				that.setData({
					wx_nickname: res.userInfo.nickName,
					wx_avatar: res.userInfo.avatarUrl,
					phone: res.userInfo.phone
				})
			}
		})
	},
})
